package com.learning.rpc.remoting.netty;

import com.learning.rpc.remoting.Handler;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;

/**
 * ClassName: NettyHandler
 * Description: 处理器
 * 真正处理
 * Date: 2022/5/10 22:54 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
public class NettyHandler extends ChannelDuplexHandler {

    private Handler handler;

    public NettyHandler(Handler handler) {
        this.handler = handler;
    }

    /**
     * 入栈事件（收到数据，请求/响应）
     *
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        handler.onReceive(new NettyChannel(ctx.channel()), msg);
    }
}
